Last updated: 2024-05-17

Checks: 6 1

Knit directory: RA_SingleCellAnalysis/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20240328) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 899450e. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/cellbender_data_h5/
    Ignored:    output/00_sce_DataPreparation.rds
    Ignored:    output/01_sce_QualityControl.rds
    Ignored:    output/01_sce_QualityControl_filtered.rds
    Ignored:    output/02_sce_DimensionalityReduction.rds
    Ignored:    output/03_sce_Integration_Batchelor.rds
    Ignored:    output/03_sce_Integration_Harmony.rds
    Ignored:    output/04_sce_Clustering.rds
    Ignored:    output/05_sce_CelltypeAnnotation.rds
    Ignored:    output/06_sce_SC_Endothelial.rds
    Ignored:    output/06_sce_SC_Fibroblast.rds
    Ignored:    output/06_sce_SC_Myeloid.rds
    Ignored:    output/06_sce_SC_Tcell.rds
    Ignored:    output/08_DifferentialAbundance_miloR.rds
    Ignored:    output/Cluster_Marker_Genes.xlsx
    Ignored:    output/MAST_KneeVsMCP.csv
    Ignored:    output/MAST_KneeVsWrist.csv
    Ignored:    output/MAST_MCPVsWrist.csv

Unstaged changes:
    Modified:   analysis/00_DataPreparation.Rmd
    Modified:   analysis/07_DifferentialExpression_Pseudobulk.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/00_DataPreparation.Rmd) and HTML (docs/00_DataPreparation.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd bdefdc5 sarloet 2024-05-15 fix
Rmd 69f37f0 sarloet 2024-05-14 fix
Rmd 14ca55c sarloet 2024-05-13 fix
html 14ca55c sarloet 2024-05-13 fix
Rmd d18442a sarloet 2024-05-06 fix
html d18442a sarloet 2024-05-06 fix
Rmd cd584b6 sarloet 2024-04-30 fix
html 3f073e5 sarloet 2024-04-30 Build site.
Rmd aa29156 sarloet 2024-04-30 Fix
html fc17296 sarloet 2024-04-23 Build site.
Rmd 6ac7e20 sarloet 2024-04-23 Fix
Rmd f73b2cd sarloet 2024-04-19 fix
html b1fc000 sarloet 2024-04-19 Build site.
Rmd 3f5d694 sarloet 2024-04-19 Rerun
Rmd c889cbe sarloet 2024-04-11 fix
Rmd 530ec4b sarloet 2024-04-05 fix
Rmd 64ae186 sarloet 2024-04-05 fix
html 5067fa6 sarloet 2024-04-04 Build site.
html fa0211e sarloet 2024-04-04 Build site.
Rmd d41ba14 sarloet 2024-04-03 fix
html bc77174 sarloet 2024-04-03 Build site.
Rmd 8543968 sarloet 2024-04-03 fix
Rmd 960ebd0 sarloet 2024-04-03 fix
Rmd eb121be sarloet 2024-04-03 fix
html ef4c495 sarloet 2024-04-02 Build site.
Rmd 9e3e00a sarloet 2024-04-02 commit changes
Rmd d291fd3 sarloet 2024-04-02 initial commit
html d291fd3 sarloet 2024-04-02 initial commit

Data Preparation and Overview

Setup

Standard packages

library(here)
source(here("code", "standard_libraries.R"))

Additional Packages

suppressPackageStartupMessages({
library(DropletUtils)
library(scDblFinder)
})

Set parameter

set.seed(123)
bpp <- BiocParallel::MulticoreParam(parallel::detectCores()-1, RNGseed=123)
path <- here::here()

Load Data

Load Cellbender output matrices

Load in the filtered feature barcode matrices from Cellbender of each sample and save them as sce object.

#Load Dataset
rawdata_folder <- paste0(path,"/data/cellbender_data_h5/")

#Get file names
filenames <- list.files(rawdata_folder ,recursive = F, full.names = F,pattern = "\\_filtered.h5$")
filepaths <- paste(rawdata_folder,filenames,sep = "")

#Load data as sce object
sce<-read10xCounts(samples = filepaths, sample.names = filenames,col.names=T,type="HDF5")

#Edit Filenames 
colData(sce)$Sample = sub("\\_w_introns.*", "", sub("cellbender_output_", "", as.character(colData(sce)$Sample)))


sce
class: SingleCellExperiment 
dim: 36601 119652 
metadata(1): Samples
assays(1): counts
rownames(36601): ENSG00000243485 ENSG00000237613 ... ENSG00000278817
  ENSG00000277196
rowData names(3): ID Symbol Type
colnames(119652): 1_AAGTACCGTCTCTCAC-1 1_GACTCTCAGGTAACTA-1 ...
  17_AGGTGTTGTGGCATCC-1 17_GCCAGCACACAAGTGG-1
colData names(2): Sample Barcode
reducedDimNames(0):
mainExpName: NULL
altExpNames(0):

Load and add Metadata

#Metadata path
metadata_folder <- paste0(path,"/data/metadata/Metadata_Master.csv")

#Load metadata
metadata_df <-read.csv(metadata_folder, header=TRUE, sep=",")
metadata_df
#Add metadata to sce
colData(sce) <- dplyr::left_join(as.data.frame(colData(sce)),
                                   metadata_df, 
                                   by= c("Sample" = "Sample"),
                                   suffix=c(".x",".y")) %>% 
      #dplyr::select(Sample, Barcode, Diagnosis, sex, Age, Joint.Location, protocol, Pathotype,Krenn total score,) %>% 
      dplyr::select(-one_of("Comments")) %>% #select all except
      DataFrame(row.names=colnames(sce))

#Set Sample names
names(colData(sce))[which(names(colData(sce))=="Sample")]="Orig.Identifier"
names(colData(sce))[which(names(colData(sce))=="ID")]="Sample"

#make col names unique
colnames(sce) <- paste0(sce$Sample, ".", sce$Barcode)

Annotate and Update genes

#Change Name
names(rowData(sce))[which(names(rowData(sce))=="ID")]="ENSEMBL"

#Annotate genes
AnnoGene <- annotate_genes(data.frame(rowData(sce)), gene_col = "Symbol")

#Add Annotated and Updated genes
rowData(sce) <- AnnoGene

#make row and col names unique
rownames(sce) <- paste0(rowData(sce)$ENSEMBL, ".", rowData(sce)$Symbol)
#Dimensions of count matrix
dim(sce)
[1]  36601 119652

Explore dataset

Dimensions of the count matrix

#Feautures/row data
data.frame(colData(sce))
#Droplet details / row data
data.frame(rowData(sce))

Exploratory plots

Histogramm with number of cells

Show the number of cells detected in each sample or joint location before filtering

Per Sample

#Histogramm with number of cells per sample
ggplot(colData(sce), aes(x=Sample))+geom_bar()+ coord_flip()+ ggtitle("Number of cells per sample") + theme_bw()

Version Author Date
d18442a sarloet 2024-05-06
984bfbe sarloet 2024-05-02
fc17296 sarloet 2024-04-23
b1fc000 sarloet 2024-04-19
5067fa6 sarloet 2024-04-04
bc77174 sarloet 2024-04-03
d291fd3 sarloet 2024-04-02
data.frame(as.list(table(colData(sce)$Sample)))

Per Joint Location

#Histogramm with number of cells per sample
ggplot(colData(sce), aes(x=Joint.Location))+geom_bar()+ coord_flip()+ ggtitle("Number of cells per sample") + theme_bw()

Version Author Date
d18442a sarloet 2024-05-06
984bfbe sarloet 2024-05-02
fc17296 sarloet 2024-04-23
b1fc000 sarloet 2024-04-19
5067fa6 sarloet 2024-04-04
bc77174 sarloet 2024-04-03
data.frame(as.list(table(colData(sce)$Joint.Location)))

Plot number of genes detected per cell

This plot shows cell counts per sample / count occurrence

#Number of genes detected per cell
#Total UMI for a gene versus the number of times detected
genesPerCell <- colSums(counts(sce) > 0)
plot(density(genesPerCell), xlab="Genes per cell", main="Number of genes detected per cell")

Version Author Date
d18442a sarloet 2024-05-06
984bfbe sarloet 2024-05-02
b1fc000 sarloet 2024-04-19
5067fa6 sarloet 2024-04-04
bc77174 sarloet 2024-04-03
d291fd3 sarloet 2024-04-02

Plot transcript capture efficiency

This plot gives an idea about the sequencing depth and if the sequencing has reached saturation or not. Plotted is the total gene count across all cells (x-axis) vs Proportion of cells the gene is detected in (y-axis) where each dot represents a gene.

#transcript_capture_efficiency
#Total UMI for a gene versus the number of times detected
tmpCounts <- counts(sce)

plot(rowSums(tmpCounts),
     rowMeans(tmpCounts > 0),
     log = "x",
     xlab="total number of UMIs",
     ylab="proportion of cells expressing the gene",
     main="Total UMI for a gene vs times detected")

Version Author Date
d18442a sarloet 2024-05-06
984bfbe sarloet 2024-05-02
b1fc000 sarloet 2024-04-19
5067fa6 sarloet 2024-04-04
bc77174 sarloet 2024-04-03
d291fd3 sarloet 2024-04-02

Initial Filtering

Initial filtering to remove unexpressed genes and cells with very low number of counts. The input to scDblFinder should not include empty droplets, and it might be necessary to remove cells with a very low coverage (e.g. <200 reads) to avoid errors. Further quality filtering should be performed downstream of doublet detection.

#Initial filtering before droplet removal
dim_before_filtering <- dim(sce)

#Get only the detected Genes
#Remove Cells with very low counts of less than 150 and genes not expressed 
sce <- sce[rowSums(counts(sce)> 0) > 0, colSums(counts(sce)> 0) > 0] #200


dim_after_filtering <- dim(sce)

cat("NR of Cells Before Initial Filtering: ", dim_before_filtering[2],
    "\nNR of Cells After Initial Filtering: ", dim_after_filtering[2],
    "\nNR of Cells Filtered out: ", dim_before_filtering[2] - dim_after_filtering[2],
    "\nCells Filtered out: [%]", (dim_before_filtering[2] - dim_after_filtering[2])/dim_before_filtering[2]*100,
    
    "\n\nNR of Genes Before Initial Filtering: ", dim_before_filtering[1],
    "\nNR of Genes After Initial Filtering: ", dim_after_filtering[1],
    "\nNR of Genes Filtered out: ", dim_before_filtering[1] - dim_after_filtering[1],
    "\nGenes Filtered out: [%]", (dim_before_filtering[1] - dim_after_filtering[1])/dim_before_filtering[1]*100
    )
NR of Cells Before Initial Filtering:  119652 
NR of Cells After Initial Filtering:  119651 
NR of Cells Filtered out:  1 
Cells Filtered out: [%] 0.000835757 

NR of Genes Before Initial Filtering:  36601 
NR of Genes After Initial Filtering:  34594 
NR of Genes Filtered out:  2007 
Genes Filtered out: [%] 5.483457

Doublet Detection

Doublets are defined as two cells that are sequenced under the same cellular barcode, which happens if they were captured in the same droplet. The scDblFinder method combines the strengths of various doublet detection approaches, training an iterative classifier on the neighborhood of real cells and artificial doublets.

Detection

#Detection
sce <- scDblFinder::scDblFinder(sce, samples=sce$Sample, BPPARAM = bpp) 

table(sce$scDblFinder.class)

singlet doublet 
 106065   13586 
as.data.frame.matrix(table(sce$Sample,sce$scDblFinder.class))

Doublet Detection Plots

Singlet/Doublet Histogramm

Absolute comparison
#Plot singlet/doublet histogramm Absolute comparison / by sample
as.data.frame(colData(sce)) %>%
  dplyr::group_by(Sample, scDblFinder.class) %>%
  dplyr::summarise(Freq=n()) %>%
  ggplot(aes(x=Sample, y=Freq, fill=scDblFinder.class,label=Freq)) + 
    geom_bar(stat="identity") +
    labs(title="Doublet detection results",
    subtitle="By sample",x="",y="Number of Cells") +
    geom_text(size=3, position = position_stack(vjust=0.5))+
    theme(axis.text.x = element_text(angle = 45,hjust=1), axis.line = element_line(colour = "black"), panel.background = element_rect(fill = NA)) 

Version Author Date
d18442a sarloet 2024-05-06
Relative comparison
# Plot singlet/doublet histogramm Relative comparison / by sample
as.data.frame(colData(sce)) %>%
  dplyr::group_by(Sample, scDblFinder.class) %>%
  dplyr::summarise(Freq=n()) %>% 
  ggplot(aes(x=Sample, y=Freq, fill=scDblFinder.class, label=Freq)) +
    geom_bar(stat="identity", position="fill") +
    labs(title="Doublet detection results",
       subtitle="By sample",
       x="Sample",
       y="Number of cells") + 
    geom_text(size=2.5, position = position_fill(vjust=0.5)) +
    theme(axis.text.x = element_text(angle = 45,hjust=1), axis.line = element_line(colour = "black"), panel.background = element_rect(fill = NA)) 

Version Author Date
d18442a sarloet 2024-05-06

Singlet/Doublet Scatter

sce <- addPerCellQC(sce)

#Plot singlet/doublet qcplots
colData(sce) %>% 
    as.data.frame() %>% 
    arrange(scDblFinder.class) %>% 
    ggplot(aes(x = sce$sum, y = sce$detected )) +
      geom_point(aes(colour = scDblFinder.class)) + 
      facet_wrap(vars(Sample))+ 
      labs(title="Total number of detected genes plotted against total number of UMIs",
       x="Total counts",
       y="Detected genes") +
      theme(strip.background=element_rect(fill="white"), panel.background = element_rect(fill = NA),axis.line = element_line(colour = "black")) 

Version Author Date
d18442a sarloet 2024-05-06

Apply Doublet Removal

print(paste0("NR of Cells Before Doublet Removal ", dim(sce)[2]))
[1] "NR of Cells Before Doublet Removal 119651"
#Apply Doublet Removal
sce <- sce[ ,sce$scDblFinder.class == "singlet"]

print(paste0("NR of Cells After Doublet Removal ", dim(sce)[2]))
[1] "NR of Cells After Doublet Removal 106065"

Save the dataset

saveRDS(sce, file =paste0(path,'/output/00_sce_DataPreparation.rds'))

sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Warsaw
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] scDblFinder_1.16.0          DropletUtils_1.22.0        
 [3] tidyr_1.3.1                 org.Hs.eg.db_3.18.0        
 [5] AnnotationDbi_1.64.1        clusterProfiler_4.10.1     
 [7] gridExtra_2.3               scran_1.30.2               
 [9] scater_1.30.1               scuttle_1.12.0             
[11] SingleCellExperiment_1.24.0 SummarizedExperiment_1.32.0
[13] Biobase_2.62.0              GenomicRanges_1.54.1       
[15] GenomeInfoDb_1.38.8         IRanges_2.36.0             
[17] S4Vectors_0.40.2            BiocGenerics_0.48.1        
[19] MatrixGenerics_1.14.0       matrixStats_1.3.0          
[21] dplyr_1.1.4                 ggplot2_3.5.1              
[23] BiocParallel_1.36.0         here_1.0.1                 
[25] workflowr_1.7.1            

loaded via a namespace (and not attached):
  [1] splines_4.3.3             later_1.3.2              
  [3] BiocIO_1.12.0             bitops_1.0-7             
  [5] ggplotify_0.1.2           R.oo_1.26.0              
  [7] tibble_3.2.1              polyclip_1.10-6          
  [9] XML_3.99-0.16.1           lifecycle_1.0.4          
 [11] edgeR_4.0.16              rprojroot_2.0.4          
 [13] processx_3.8.4            lattice_0.22-5           
 [15] MASS_7.3-60.0.1           magrittr_2.0.3           
 [17] limma_3.58.1              sass_0.4.9               
 [19] rmarkdown_2.26            jquerylib_0.1.4          
 [21] yaml_2.3.8                metapod_1.10.1           
 [23] httpuv_1.6.15             cowplot_1.1.3            
 [25] DBI_1.2.2                 RColorBrewer_1.1-3       
 [27] abind_1.4-5               zlibbioc_1.48.2          
 [29] R.utils_2.12.3            purrr_1.0.2              
 [31] ggraph_2.2.1              RCurl_1.98-1.14          
 [33] yulab.utils_0.1.4         tweenr_2.0.3             
 [35] git2r_0.33.0              GenomeInfoDbData_1.2.11  
 [37] enrichplot_1.22.0         ggrepel_0.9.5            
 [39] irlba_2.3.5.1             tidytree_0.4.6           
 [41] dqrng_0.3.2               DelayedMatrixStats_1.24.0
 [43] codetools_0.2-19          DelayedArray_0.28.0      
 [45] DOSE_3.28.2               ggforce_0.4.2            
 [47] tidyselect_1.2.1          aplot_0.2.2              
 [49] farver_2.1.2              ScaledMatrix_1.10.0      
 [51] viridis_0.6.5             GenomicAlignments_1.38.2 
 [53] jsonlite_1.8.8            BiocNeighbors_1.20.2     
 [55] tidygraph_1.3.1           tools_4.3.3              
 [57] treeio_1.26.0             Rcpp_1.0.12              
 [59] glue_1.7.0                SparseArray_1.2.4        
 [61] xfun_0.43                 qvalue_2.34.0            
 [63] HDF5Array_1.30.1          withr_3.0.0              
 [65] fastmap_1.1.1             rhdf5filters_1.14.1      
 [67] bluster_1.12.0            fansi_1.0.6              
 [69] callr_3.7.6               digest_0.6.35            
 [71] rsvd_1.0.5                R6_2.5.1                 
 [73] gridGraphics_0.5-1        colorspace_2.1-0         
 [75] GO.db_3.18.0              RSQLite_2.3.6            
 [77] R.methodsS3_1.8.2         utf8_1.2.4               
 [79] generics_0.1.3            data.table_1.15.4        
 [81] rtracklayer_1.62.0        graphlayouts_1.1.1       
 [83] httr_1.4.7                S4Arrays_1.2.1           
 [85] scatterpie_0.2.2          whisker_0.4.1            
 [87] pkgconfig_2.0.3           gtable_0.3.5             
 [89] blob_1.2.4                XVector_0.42.0           
 [91] shadowtext_0.1.3          htmltools_0.5.8.1        
 [93] fgsea_1.28.0              scales_1.3.0             
 [95] png_0.1-8                 ggfun_0.1.4              
 [97] knitr_1.45                rstudioapi_0.16.0        
 [99] rjson_0.2.21              reshape2_1.4.4           
[101] nlme_3.1-164              rhdf5_2.46.1             
[103] cachem_1.0.8              stringr_1.5.1            
[105] parallel_4.3.3            vipor_0.4.7              
[107] HDO.db_0.99.1             restfulr_0.0.15          
[109] pillar_1.9.0              grid_4.3.3               
[111] vctrs_0.6.5               promises_1.3.0           
[113] BiocSingular_1.18.0       beachmat_2.18.1          
[115] cluster_2.1.6             beeswarm_0.4.0           
[117] evaluate_0.23             Rsamtools_2.18.0         
[119] cli_3.6.2                 locfit_1.5-9.9           
[121] compiler_4.3.3            rlang_1.1.3              
[123] crayon_1.5.2              labeling_0.4.3           
[125] ps_1.7.6                  getPass_0.2-4            
[127] plyr_1.8.9                fs_1.6.4                 
[129] ggbeeswarm_0.7.2          stringi_1.8.4            
[131] viridisLite_0.4.2         munsell_0.5.1            
[133] Biostrings_2.70.3         lazyeval_0.2.2           
[135] GOSemSim_2.28.1           Matrix_1.6-5             
[137] patchwork_1.2.0           sparseMatrixStats_1.14.0 
[139] bit64_4.0.5               Rhdf5lib_1.24.2          
[141] KEGGREST_1.42.0           statmod_1.5.0            
[143] highr_0.10                igraph_2.0.3             
[145] memoise_2.0.1             bslib_0.7.0              
[147] ggtree_3.10.1             fastmatch_1.1-4          
[149] xgboost_1.7.7.1           bit_4.0.5                
[151] ape_5.8                   gson_0.1.0